package com.shop.servlet;

import com.shop.model.Student;
import com.shop.service.IOrderService;
import com.shop.service.IStudentService;
import com.shop.service.OrderService;
import com.shop.service.StudentService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/updateIdentity")
public class UpdateIdentityServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String studentId = req.getParameter("studentId");
        System.out.println("UpdateIdentityServlet:19:"+studentId);

        IStudentService istudentService = new StudentService();
        Student student = istudentService.search(studentId);
        IOrderService iorderService = new OrderService();
        double money1 = student.getMoney();
        double money = money1-188.0;
        if(money>=0) {
            iorderService.updateMoney1(studentId, money);//更新余额
            istudentService.updateIdentity(studentId);//更新身份
            req.setAttribute("studentId", studentId);
            req.getRequestDispatcher("welcome.jsp").forward(req, resp);
        }else if(money<0){
            req.setAttribute("studentId", studentId);
            req.setAttribute("error", "账户余额不足，请充值！");
            req.getRequestDispatcher("Error.jsp").forward(req, resp);
        }
    }
}
